import 'package:chop/common/MessageUtils.dart';
import 'package:chop/common/SPUtils.dart';
import 'package:chop/common/StringUtils.dart';
import 'package:chop/view/login/login.dart';
import 'package:chop/view/order/order_page.dart';
import 'package:chop/widgets/LoginOutWidget.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
//import 'package:getflutter/getflutter.dart';
import 'package:loading_animations/loading_animations.dart';

class DriverPersonal_center extends StatefulWidget {

  @override
  _DriverPersonal_centerState createState() => _DriverPersonal_centerState();
}

class _DriverPersonal_centerState extends State<DriverPersonal_center> {
  String _username; //用户昵称
  String _ID; //用户昵称
  bool isNetWorkBad = false;

  @override
  void initState() {
    // TODO: implement initState
    getUser();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        childView: BaseTitleBar(
          "个人中心",
        ),
        preferredSize: Size.fromHeight(65.0),
      ),
      body: getBody(),
      backgroundColor: Colors.white,
    );
  }

  getBody() {
    if (isNetWorkBad) {
      return ListView(
        children: <Widget>[_buildBody()],
      );
    } else {
      return Center(child: LoadingJumpingLine.square());
    }
  }

  getUser() async {
    _username = await SPUtils.get("phone");
    _ID = await SPUtils.get("phone");
    setState(() {
      isNetWorkBad = true;
    });
  }

  _buildBody() {
    return Container(
      padding: EdgeInsets.all(20),
      width: double.infinity,
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                //个人信息
                  padding: EdgeInsets.all(0),
                  height: ScreenUtil().setWidth(180),
                  width: ScreenUtil().setWidth(500),
                  child: Column(
                    children: <Widget>[
                      GestureDetector(
                        child: Column(
                          children: <Widget>[
//                            GFListTile(
//                              avatar: GFAvatar(
//                                child: Image.network(
//                                    "https://dss0.baidu.com/73t1bjeh1BF3odCf/it/u=1362163043,768221560&fm=85&s=F8A2B353DF436ACE4C4DECD6030050F2"),
//                              ),
//                              titleText:
//                              '${StringUtils.isNull(_username) ? "请登录" : _username}',
//                              subtitleText: 'ID:${_ID}',
//                            ),
                          ],
                        ),
                        onTap: () {
                          if (StringUtils.isNull(_username)) {
                            Get.off(LoginPage());
                          }
                        },
                      ),
                    ],
                  )),
            ],
          ),
          Column(
            children: <Widget>[
              Container(
                //我的订单
                margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
                padding: EdgeInsets.only(top: 20),
                height: ScreenUtil().setHeight(180),
                width: ScreenUtil().setWidth(700),
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.blue, width: 1),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black12,
                        offset: Offset(5.0, 5.0),
                      ),
                    ]),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child:  Center(
//                        margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
                        child: Column(
                          children: <Widget>[
                            getOrderItem("待支付",
                                icon: Icon(
                                  Icons.monetization_on,
                                  color: Colors.blue,
                                  size: ScreenUtil().setSp(60),
                                ),
                                callback: (){
                                  Get.to(orderPage(type:2,));
                                }
                            )
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: Center(
                        child: Column(
                          children: <Widget>[
                            getOrderItem("待接单",
                                icon: Icon(
                                  Icons.access_alarm,
                                  color: Colors.blue,
                                  size: ScreenUtil().setSp(60),
                                ),
                                callback: (){
                                  Get.to(orderPage(type:3,));
                                })
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child: Center(
                        child: Column(
                          children: <Widget>[
                            getOrderItem("进行中",
                                icon: Icon(
                                  Icons.local_car_wash,
                                  color: Colors.blue,
                                  size: ScreenUtil().setSp(60),
                                ),
                                callback: (){
                                  Get.to(orderPage(type:0,));
                                })
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      child:
                      Center(
                        child: Column(
                          children: <Widget>[
                            getOrderItem("已完成",
                                icon: Icon(
                                  Icons.done_all,
                                  color: Colors.blue,
                                  size: ScreenUtil().setSp(60),
                                ),
                                callback: (){
                                  Get.to(orderPage(type:1,));
                                })
                          ],
                        ),
                      ) ,
                    ),
                  ],
                ),
              ),
              //我的钱包
              Container(
                margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
                width: double.infinity,
                child: Column(
                  children: <Widget>[
                    getPersonItem("我的钱包",icon: Icon(Icons.account_balance_wallet,color: Colors.blue,size: 25,),callback: (){
//                      Get.to(WebSocketDemo());
                    }),
                    getPersonItem("设置中心",icon: Icon(Icons.brightness_high,color: Colors.blue,size: 25,),callback: (){
//                      Get.to(Setting_center());
                    }),
                    //发单设置
                    getPersonItem("发单设置",
                        icon: Icon(
                          Icons.description,
                          color: Colors.blue,
                          size: 25,
                        ),
                        callback: () {}),
                    //我的优惠卷
                    getPersonItem("优惠卷",
                        icon: Icon(
                          Icons.payment,
                          color: Colors.blue,
                          size: 25,
                        ),
                        callback: () {}),
                    getPersonItem("骑手黑名单",
                        icon: Icon(
                          Icons.https,
                          color: Colors.blue,
                          size: 25,
                        ),
                        callback: () {}),
                    Container(
                      height: 50,
                    ),
                    Center(
                      child: RaisedButton(
                        child: FixedSizeText(
                          "${StringUtils.isNull(_username) ? '立即登录' : '退出登录'}",
                          style: TextStyle(fontSize: 16),
                        ),
                        onPressed: () {
                          if(StringUtils.isNull(_username)){
                            SPUtils.clear();
                            Get.off(LoginPage());
                          }else{
                            LoginOut(context,text: "退出登录",click: (){
                              SPUtils.clear();
                              MsgUtils.colseChannel();
                              Get.off(LoginPage());
                            },
                                callback: (){
                                  Get.back();
                                });
                          }
                        },
                        color: StringUtils.isNull(_username)
                            ? Colors.blue
                            : Colors.redAccent,
                        padding: EdgeInsets.only(
                            left: 60, top: 10, right: 60, bottom: 10),
                        textColor: Colors.white,
                        textTheme: ButtonTextTheme.accent,
                      ),
                    )
                  ],
                ),
              ),
              //设置中心
            ],
          ),
        ],
      ),
    );
  }

  Widget getPersonItem(String title, {icon, VoidCallback callback}) {
    return GestureDetector(
      child: Container(
        margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
        height: ScreenUtil().setHeight(80),
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Container(
                margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                alignment: Alignment.topRight,
                child: Column(
                  children: <Widget>[icon],
                ),
              ),
            ),
            Expanded(
              flex: 4,
              child: Container(
                alignment: Alignment.topLeft,
                margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: Column(
                  children: <Widget>[
                    FixedSizeText(
                      "    ${title}",
                      style: TextStyle(
                        fontSize: 16,
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 5,
              child: Container(
                margin: EdgeInsets.fromLTRB(0, 10, 10, 0),
                alignment: Alignment.topRight,
                child: Column(
                  children: <Widget>[
                    Icon(
                      Icons.arrow_forward,
                      color: Colors.blue,
                      size: 25,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
      onTap: callback,
    );
  }

  Widget getOrderItem(String text, {Icon icon, VoidCallback callback}) {
    return GestureDetector(
      child: Container(
        child: Column(
          children: <Widget>[
            icon,
            FixedSizeText(
              "${text}",
              style: TextStyle(fontSize: 15),
            ),
          ],
        ),
      ),
      onTap: callback,
    );
  }
}
